/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.qb9.flashlib.display
{
	import com.qb9.flashlib.geom.Anchor;
	
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	
	/**
	 * Camera that accepts an array of layers and keeps them positioned in relation to the canvas.
	 * It's useful to generate a side scrolling effect, the wider the layer, the faster it moves.
	 */
	
	public class MultiLayerCamera extends Camera2D
	{
		protected var layers:Array = [];
		
		public function MultiLayerCamera( canvas:DisplayObject, layers:Array, viewport:Rectangle = null, anchor:Anchor = null )
		{
			this.layers = layers;
			
			super(canvas, viewport, anchor);
		}

		override public function set x( v:Number ) : void
		{
			super.x = v;
			
			var base:Number = viewportWidth;
			var rest:Number = canvas.width - base;
			
			for each (var layer:DisplayObject in layers)
				layer.x = canvas.x * ((layer.width - base) / rest);
		}
		
		// TODO: The y isn't that simple, one should specify some kind of anchor
		override public function set y( v:Number ) : void
		{
			super.y = v;
			
			var base:Number = viewportHeight;
			var rest:Number = canvas.height - base;
			
			for each (var layer:DisplayObject in layers)
				layer.y = canvas.y * ((layer.height - base) / rest);
		}
		
		override public function set zoom(val:Number):void
		{
			for each (var layer:DisplayObject in layers)
				layer.scaleX = layer.scaleY = val;
			super.zoom = val;
		} 
	}
}